<?php
/*
 * This is a page to list information of all pages for publiser.
 *   List with following data.
 *   Title, Date Authored, Author, Date Modified, Modified By, Description, Publish, Delete
 *
 */
require_once('includes/dbfunctions.php');

if (!isset($_SESSION['User'])) {
    require ("login.php");
    exit;
}
//If the form is submitted, proceed
if ($_POST)
{
    if (isset($_POST['publish']))
    {
        $pages = $_POST['publish'];

        //for every user approved
        for ($i=0; $i < count($pages); $i++)
        {
			approveContent($pages[$i], $_SESSION['User'], contentRoot);
        }
    }
}

if ($_GET['id'] != null) {
	deleteContent($_GET['id']);
}

?>
<h3>Pages awaiting publishing</h3>
<form name="publishPages" method="" action="">
    <table width="700" id="publisherInfo">
        <tr> <td colspan="5">
                <span class="pub_success">Publishing Successful</span>
                <span class="pub_error">Error, no pages selected</span>
            </td>
        </tr>
        <tr>
            <th scope="col">Title</th>
            <th scope="col">Date Submitted</th>
            <th scope="col">User</th>
            <th scope="col">Description</th>
            <th scope="col">Publish</th>
            <th scope="col">Delete</th>
        </tr>

        <?php
        $publisherInfo = getPublisherInfo();

        $i = count($publisherInfo) - 1;
        while ($i >= 0) {
            if ($publisherInfo[$i]['Approve'] == 0) {
                echo '<tr>';
                echo '<td scope="col"><a href="../preview.php?page='.$publisherInfo[$i]['Alias'].'" target="_blank">';
                echo $publisherInfo[$i]['Title']."</a></td>";

                list($date, $time) = split(" ", $publisherInfo[$i]['Date']);
                list($year, $month, $day) = split("-", $date);
                echo "<td scope=\"col\">".$month."/".$day."/".$year."</td>";

                echo "<td scope=\"col\">".$publisherInfo[$i]['Author']."</td>";

                echo "<td scope=\"col\">".$publisherInfo[$i]['Desc']."</td>";

                $alias = $publisherInfo[$i]['Alias'];
                echo "<td scope=\"col\"><input type=\"checkbox\" name=\"publish[]\" value=\"$alias\"></td>";

				echo "<td scope=\"col\">";
				echo "<FORM>";
				echo "<INPUT TYPE='BUTTON' VALUE='Delete' ONCLICK=\"window.location.href='deleteContent.php?alias=".$publisherInfo[$i]['Alias']."&flag=0'\">";
				echo "</FORM>";
				echo "</td>";

                echo '</tr>';
            } else if (trim($publisherInfo[$i]['LastFilename']) != "") {
                echo '<tr>';
                echo '<td scope="col"><a href="preview.php?page='.$publisherInfo[$i]['Alias'].'&revision=1" target="_blank">';
                echo $publisherInfo[$i]['Title']."</a></td>";

                list($date, $time) = split(" ", $publisherInfo[$i]['LastDate']);
                list($year, $month, $day) = split("-", $date);
                echo "<td scope=\"col\">".$month."/".$day."/".$year."</td>";

                echo "<td scope=\"col\">".$publisherInfo[$i]['LastUser']."</td>";

                echo "<td scope=\"col\">".$publisherInfo[$i]['LastDesc']."</td>";

                $alias = $publisherInfo[$i]['Alias'];
                echo "<td scope=\"col\"><input type=\"checkbox\" name=\"publish[]\" value=\"$alias\"></td>";

				echo "<td scope=\"col\">";
				echo "<FORM>";
				echo "<INPUT TYPE='BUTTON' VALUE='Delete' ONCLICK=\"window.location.href='deleteContent.php?alias=".$publisherInfo[$i]['Alias']."&flag=1'\">";
				echo "</FORM>";
				echo "</td>";

                echo '</tr>';
            }
            $i--;
        }
        if (count($publisherInfo) > 0) {
            echo ('<tr><td colspan="6"><input id="publishButton" name="submit" type="button" value="Publish Selected" class="publish"></td></tr>');
        } else {
            echo ('<tr><td colspan="6">There are no pages waiting to be published.</td></tr>');
        }

        ?>
    </table>
</form>

